]> git.neil.brown.name Git - wiggle.git/blob - split.c
Add heuristic to take shortcut when too slow.
[wiggle.git] / split.c
1 /*
2  * wiggle - apply rejected patches
3  *
4  * Copyright (C) 2003-2013 Neil Brown <neilb@suse.de>
5  *
6  *
7  *    This program is free software; you can redistribute it and/or modify
8  *    it under the terms of the GNU General Public License as published by
9  *    the Free Software Foundation; either version 2 of the License, or
10  *    (at your option) any later version.
11  *
12  *    This program is distributed in the hope that it will be useful,
13  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *    GNU General Public License for more details.
16  *
17  *    You should have received a copy of the GNU General Public License
18  *    along with this program.
19  *
20  *    Author: Neil Brown
21  *    Email: <neilb@suse.de>
22  */
23
24 /*
25  * Split a stream into words or lines
26  *
27  * A word is one of:
28  *    string of [A-Za-z0-9_]
29  *    or string of [ \t]
30  *    or single char (i.e. punctuation and newlines).
31  *
32  * A line is any string that ends with \n
33  *
34  * As a special case to allow proper aligning of multiple chunks
35  * in a patch, a word starting \0 will include 20+ chars with a newline
36  * second from the end.
37  *
38  * We make two passes through the stream.
39  * Firstly we count the number of item so an array can be allocated,
40  * then we store start and length of each item in the array
41  *
42  */
43
44 #include        "wiggle.h"
45 #include        <stdlib.h>
46 #include        <ctype.h>
47 #include        <stdlib.h>
48
49 #include "ccan/hash/hash.h"
50
51 static int split_internal(char *start, char *end, int type,
52                           struct elmnt *list)
53 {
54         int cnt = 0;
55
56         while (start < end) {
57                 char *cp = start;
58                 char *cp2;
59                 int prefix = 0;
60
61                 if ((type & ByWord) && (type & IgnoreBlanks))
62                         while (cp < end &&
63                                (*cp == ' ' || *cp == '\t')) {
64                                 prefix++;
65                                 cp++;
66                         }
67                 start = cp;
68
69                 if (*cp == '\0' && cp+19 < end) {
70                         /* special word */
71                         cp += 19;
72                         cp += strlen(cp) + 1;
73                 } else
74                         switch (type & ByMask) {
75                         case ByLine:
76                                 while (cp < end && *cp != '\n')
77                                         cp++;
78                                 if (cp < end)
79                                         cp++;
80                                 break;
81                         case ByWord:
82                                 if (*cp == ' ' || *cp == '\t') {
83                                         do
84                                                 cp++;
85                                         while (cp < end
86                                                && (*cp == ' '
87                                                    || *cp == '\t'));
88                                 } else if ((type & WholeWord) ||
89                                            isalnum(*cp) || *cp == '_') {
90                                         do
91                                                 cp++;
92                                         while (cp < end
93                                                && (((type & WholeWord)
94                                                     && *cp != ' ' && *cp != '\t'
95                                                     && *cp != '\n')
96                                                    || isalnum(*cp)
97                                                    || *cp == '_'));
98                                 } else
99                                         cp++;
100                                 break;
101                         }
102                 cp2 = cp;
103                 if ((type & ByWord) && (type & IgnoreBlanks) &&
104                     *start && *start != '\n')
105                         while (cp2 < end &&
106                                (*cp2 == ' ' || *cp2 == '\t' || *cp2 == '\n')) {
107                                 cp2++;
108                                 if (cp2[-1] == '\n')
109                                         break;
110                         }
111                 if (list) {
112                         list->start = start;
113                         list->len = cp-start;
114                         list->plen = cp2-start;
115                         list->prefix = prefix;
116                         if (*start)
117                                 list->hash = hash(start, list->len, 0);
118                         else
119                                 list->hash = atoi(start+1);
120                         list++;
121                 }
122                 cnt++;
123                 start = cp2;
124         }
125         return cnt;
126 }
127
128 struct file split_stream(struct stream s, int type)
129 {
130         int cnt;
131         struct file f;
132         char *c, *end;
133
134         if (!s.body) {
135                 f.list = NULL;
136                 f.elcnt = 0;
137                 return f;
138         }
139         end = s.body+s.len;
140         c = s.body;
141
142         cnt = split_internal(c, end, type, NULL);
143         f.list = xmalloc(cnt*sizeof(struct elmnt));
144
145         f.elcnt = split_internal(c, end, type, f.list);
146         return f;
147 }